from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import config

app = Flask(__name__)
app.config.from_object(config)
db = SQLAlchemy(app)


class User(db.Model):
    __tablename__ = "user"
    id = db.Column('user_id', db.BigInteger, primary_key=True, autoincrement=True)
    account = db.Column(db.String(64))
    password = db.Column(db.String(225), default='')
    avatar = db.Column(db.String(64), default='')
    sex = db.Column(db.SmallInteger, default=2)
    birth_date = db.Column(db.DateTime, default=datetime.now())
    phone = db.Column(db.String(225), unique=True)
    article_count = db.Column(db.Integer, default=0)
    following_count = db.Column(db.Integer, default=0)
    fans_count = db.Column(db.Integer, default=0)
    create_time = db.Column(db.DateTime, default=datetime.now())
    update_time = db.Column(db.DateTime, default=datetime.now(),
                            onupdate=datetime.now())


class Relation(db.Model):
    __tablename__ = "relation"
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    target_user_id = db.Column(db.BigInteger, nullable=False)
    relation = db.Column(db.SmallInteger, default=1)
    user_id = db.Column(db.BigInteger, nullable=False)


class Channel(db.Model):
    __tablename__ = "channel"
    """
    新闻频道
    """
    id = db.Column('channel_id', db.Integer, primary_key=True, comment='频道ID')
    name = db.Column(db.String(64), nullable=False, unique=True)
    sequence = db.Column(db.Integer, default=0, comment='序号')
    is_visible = db.Column(db.Boolean, default=False, comment='是否可见')
    is_default = db.Column(db.Boolean, default=False, comment='是否默认')
    create_time = db.Column(db.DateTime, default=datetime.now())
    update_time = db.Column(db.DateTime, default=datetime.now(),
                            onupdate=datetime.now())


class UserChannel(db.Model):
    __tablename__ = "user_channel"
    """
    用户关注频道表
    """
    id = db.Column('user_channel_id', db.Integer, primary_key=True, comment='主键')
    user_id = db.Column(db.Integer, comment='用户ID')
    channel_id = db.Column(db.Integer, comment='频道ID')
    is_deleted = db.Column(db.Boolean, default=1, comment='是否删除')
    update_time = db.Column(db.DateTime, default=datetime.now(),
                            onupdate=datetime.now(), comment='更新时间')
    sequence = db.Column(db.Integer, default=0, comment='序号')


class Article(db.Model):
    __tablename__ = 'articles'
    """
    文章信息表
    """
    id = db.Column('articles_id', db.Integer, primary_key=True, comment='文章ID')
    user_id = db.Column(db.Integer, comment='用户ID')
    channel_id = db.Column(db.Integer, comment='所属频道id')
    title = db.Column(db.String(225), comment='标题')
    comment_count = db.Column(db.Integer, default=0, comment='评论数量')
    allow_comment = db.Column(db.Boolean, default=True, comment='是否可以评论')
    ctime = db.Column('create_time', db.DateTime, default=datetime.now, comment='创建时间')
    utime = db.Column('update_time', db.DateTime, default=datetime.now, comment='更新时间')


class ArticleContent(db.Column):
    __tablename__ = 'articles_content'
    """
    文章内容表
    """
    id = db.Column('articles_content_id', db.Integer, db.ForeignKey('article.article_id'),
                   primary_key=True, comment='文章ID')
    content = db.Column(db.Text, comment='帖子、文章内容')


class Attitude(db.Column):
    __tablename__ = 'attitude'
    """
    文章的点赞
    """

    class ATTITUDE:
        DISLIKE = 0  # 不点
        LIKING = 1  # 点赞
        COLLECT = 2  # 收藏
        NOT_COLLECT = 3  # 取消收藏

    id = db.Column('attitude_id', db.Integer, primary_key=True, comment='主键ID')
    user_id = db.Column(db.Integer, comment='用户ID')
    article_id = db.Column(db.Integer, db.ForeignKey('articles.articles_id'), comment='文章ID')
    attitude = db.Column(db.Integer, comment='点赞与收藏')
    ctime = db.Column('create_time', db.DateTime, default=datetime.now, comment='创建时间')
    utime = db.Column('update_time', db.DateTime, default=datetime.now,
                      onupdate=datetime.now, comment='更新时间')


class Comment(db.Column):
    __tablename__ = 'comments'
    """
    文章评论
    """
    id = db.Column('comments_id', db.Integer, primary_key=True, comment='评论ID')
    user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), comment='用户ID')
    article_id = db.Column(db.Integer, db.ForeignKey('articles.articles_id'), comment='文章ID')
    parent_id = db.Column(db.Integer, doc='被评论的评论id')
    content = db.Column(db.String, comment='评论内容')
    ctime = db.Column('create_time', db.DateTime, default=datetime.now, comment='创建时间')


class CommentLiking(db.Model):
    __tablename__ = 'comment_liking'
    """
    评论点赞
    """
    id = db.Column('comment_liking_id', db.Integer, primary_key=True, comment='主键ID')
    user_id = db.Column(db.Integer, comment='用户ID')
    comment_id = db.Column(db.Integer, comment='评论ID')
    ctime = db.Column('create_time', db.DateTime, default=datetime.now, comment='创建时间')
    utime = db.Column('update_time', db.DateTime, default=datetime.now,
                      onupdate=datetime.now, comment='更新时间')
    is_deleted = db.Column(db.Boolean, default=False, comment='是否取消赞')
